package com.mentaleye.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtils {
	
	/**
	 * ����ת�����ַ���
	 * @param date Ҫת��������
	 * @param formatstr ����ת����ʽ(yyyy-MM-dd HH-mm-ss)
	 * @return	�������ڶ�����ַ�����ʽ
	 */
	public static String getStrByDate(Date date,String formatstr){
		String str="";
		SimpleDateFormat dateformat=new SimpleDateFormat(formatstr);
		if(date!=null){
			str= dateformat.format(date);
		}
			return str;
		
		
	}
	
	/**
	 * �ַ���ת��������
	 * @param datestr Ҫת�����ַ���
	 * @param formatstr ����ת����ʽ(yyyy-MM-dd HH-mm-ss)
	 * @return ����ת��������ڶ���
	 */
	public static Date getDateByStr(String datestr,String formatstr){
		SimpleDateFormat dateformate=new SimpleDateFormat(formatstr);
		Date date=null;
		try {
			date=dateformate.parse(datestr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}
	public static String getFileNameByDate(String ext){
		Date today=new Date();
		String datestr=getStrByDate(today, "yyyyMMddHH");
		
		for(int i=0;i<4;i++){
			int num=(int)(Math.random()*10);
			datestr=datestr+num;
		}
		return datestr+"."+ext;
	}
	
	public static String getFileName(String xslx){
		String datestr = null;
		for(int i=0;i<4;i++){
			int num=(int)(Math.random()*3);
			datestr=datestr+num;
		}
		return datestr+"."+xslx;
	}
	
	
	public static String getFileNameByDate(){
		Date today=new Date();
		String datestr=getStrByDate(today, "MMdd");
		
		for(int i=0;i<6;i++){
			int num=(int)(Math.random()*4);
			datestr=datestr+num;
		}
		return datestr;
	}
	
	
	/**
	 * 根据年份和月份查询当月的时间集合
	 * @param years 年份
	 * @param mouth 月份
	 * @return List 时间集合
	 */
	public static List<Date> getDatesByMouth(String years,String mouth){
		String startDate = null;
    	String endDate = null;
		if(years != null && !"".equals(years) && mouth != null && !"".equals(mouth)){
    		startDate = years + "-" + mouth + "-" + "01";
        	int newmouth = Integer.valueOf(mouth)+1;
        	endDate = years + "-" +""+newmouth+"" + "-" + "01";
    	}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date beginDate = null;
        Date finishDate = null;
		try {
			beginDate = sdf.parse(startDate);
			finishDate = sdf.parse(endDate); 
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		List<Date> dateList = getDatesBetweenTwoString(beginDate, finishDate);
		dateList.remove(dateList.size() - 1);
		return dateList;
	}
	

    /** 
     * 根据开始时间和结束时间返回时间段内的时间集合 
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @return List 时间集合
     */  
    public static List<Date> getDatesBetweenTwoString(Date beginDate,Date finishDate){
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	try {
			beginDate = sdf.parse(sdf.format(beginDate));
			finishDate = sdf.parse(sdf.format(finishDate));
		} catch (ParseException e) {
			e.printStackTrace();
		}
        List<Date> lDate = new ArrayList<Date>();  
        lDate.add(beginDate);// 把开始时间加入集合  
        Calendar cal = Calendar.getInstance();  
        // 使用给定的 Date 设置此 Calendar 的时间  
        cal.setTime(beginDate);  
        boolean bContinue = true;  
        while (bContinue) {  
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量  
            cal.add(Calendar.DAY_OF_MONTH, 1);  
            // 测试此日期是否在指定日期之后  
            if (finishDate.after(cal.getTime())) {  
                lDate.add(cal.getTime());  
            } else {  
                break;  
            }  
        }  
        lDate.add(finishDate);// 把结束时间加入集合  
        return lDate;  
    }  
    
    /**
     * 根据日期获得当前日期的月份
     * @param date 日期
     * @return 返回月份
     */
    public static int getMouthByDate(Date date){
    	Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.MONTH)+1;
    }
    
    /**
     * 根据日期获得当前日期是星期几
     * @param date 日期
     * @return 返回当前为星期几
     */
    public static String getWeek(Date date){ 
		SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
		String week = sdf.format(date);
		return week;
	}
    
    /**
     * 根据一个日期字符串得到这个时间是这一年的第几周
     * @param dateString 日期字符串
     * @return 返回数字
     */
   public static int getWeekNumberByString(String dateString){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");   
		Calendar cl = Calendar.getInstance();   
		try {
			cl.setTime(sdf.parse(dateString));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
		int week = cl.get(Calendar.WEEK_OF_YEAR);
		return week;
		/*System.out.println(week);   
		cl.add(Calendar.DAY_OF_MONTH, -7);  
		int year = cl.get(Calendar.YEAR);  
		if(week<cl.get(Calendar.WEEK_OF_YEAR)){  
		    year+=1;  
		}
		System.out.println(year+"年第"+week+"周"); */
   }
   
   /**
    * 给指定日期加指定天数
    * @param date 指定的日期
    * @param day 要加的天数
    * @return 加天数后日期
    */
   public static Date addDate(Date date,long day){
	   	long time = date.getTime();
	   	day = day*24*60*60*1000;
	   	time+=day;
	   	return new Date(time);
   }

}
